package com.ccit.money.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ccit.common.client.EmployeeClient;
import com.ccit.common.exception.GymException;
import com.ccit.common.result.Result;
import com.ccit.common.utils.PageUtil;
import com.ccit.common.utils.RedisUtils;
import com.ccit.common.utils.UploadUtil;
import com.ccit.model.Type;
import com.ccit.money.service.TypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;



@Api(tags = "器材管理")
@RestController
@RequestMapping("/type")
public class TypeController {

    @Autowired
    private TypeService typeService;

    @Autowired
    private EmployeeClient employeeClient;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private UploadUtil uploadUtil;

    @ApiOperation(value = "分页查询种类")
    @GetMapping("/getTypeByPage/{current}/{size}")
    public Result getRecord(@PathVariable Integer current,
                            @PathVariable Integer size) {
        String key = "typePage:" + current + ":" + size;
        boolean hasKey = redisUtils.hasKey(key);
        Page<Type> page;
        if (hasKey) {
            page = (Page<Type>)redisUtils.get(key);
        } else {
            Page<Type> typePage = new Page<>(current, size);
            page = typeService.page(typePage);
            redisUtils.set(key, page);
        }
        return Result.ok(page);

    }

    @ApiOperation(value = "查询所有种类")
    @GetMapping("/listAll")
    public Result listAll() {
        List<Type> list;
        String key = "allType";
        boolean hasKey = redisUtils.hasKey(key);
        if (hasKey) {
            list = (List<Type> )redisUtils.get(key);
        } else {
            list = typeService.list();
            redisUtils.set(key,list);
        }
        return Result.ok(list);
    }

    @ApiOperation(value = "条件查询")
    @PostMapping("/getByDynamic/{current}/{size}")
    public Result getByDynamic(
            @RequestParam(required = false) String name,
            @PathVariable Integer current,
            @PathVariable Integer size) {
        Page<Type> page;
        String key = "TypeList"+ ":" +current + ":" + size;
        if (name != null|| name == ""){
            key = "TypeList" + ":" + name + ":" +current + ":" + size;
        }
        boolean hasKey = redisUtils.hasKey(key);
        if (hasKey){
            page = (Page<Type>)redisUtils.get(key);
        }else {
            QueryWrapper<Type> wrapper = new QueryWrapper<Type>();

            if (name != null){
                    wrapper.lambda()
                            .eq(Type::getName, name)
                            .orderByDesc(Type::getName);
                }else {
                    wrapper.lambda()
                            .orderByDesc(Type::getName);
                }
            List<Type> list = typeService.list(wrapper);
            page = PageUtil.getPages(current, size, list);
            redisUtils.set(key,page);
            }
        return Result.ok(page);
    }


    @ApiOperation(value = "添加种类")
    @PostMapping("/addType")
    public Result addIntegralGoods(@RequestBody Type type) {
        boolean result = typeService.save(type);
        if (result) {
            redisUtils.deleteRedisKeys("typePage");
            redisUtils.deleteRedisKeys("allType");
            redisUtils.deleteRedisKeys("TypeList");
            return Result.ok("添加成功");
        } else {
            return Result.fail().message("添加失败");
        }
    }

    @ApiOperation(value = "修改种类")
    @PutMapping("/updateType")
    public Result updateEquipment(@RequestBody Type type) {
        Type type1 = typeService.getById(type.getId());
        if (type1 == null){
            throw new GymException(202,"无此种类");
        }
        boolean flag = typeService.updateById(type);
        if (flag) {
            redisUtils.deleteRedisKeys("typePage");
            redisUtils.deleteRedisKeys("allType");
            redisUtils.deleteRedisKeys("TypeList");
            return Result.ok();
        } else {
            return Result.fail().message("修改失败");
        }
    }
}
